package com.gsafety.library.Net.okhttputil.interceptor;

import com.gsafety.library.Net.RequestHandler;

import org.jetbrains.annotations.NotNull;

import java.io.IOException;

import okhttp3.Interceptor;
import okhttp3.Request;
import okhttp3.Response;

/**网络拦截器
 * @author : lj
 * @date : 2020/9/21 15:55
 */
public class NetInterceptor implements Interceptor {
    private RequestHandler handler;
    public NetInterceptor(RequestHandler chandler) {
        this.handler=chandler;
    }

    /**
     * 网络请求拦截方法
     */
    @NotNull
    @Override
    public Response intercept(@NotNull Chain chain) throws IOException {
        Request request = chain.request();
        if (handler != null) {
            request = handler.onBeforeRequest(request, chain);
        }
        Response response = chain.proceed(request);
        if (handler != null) {
            return handler.onAfterRequest(response, chain);
        }

        return response;
    }
}
